package com.k.util.resourcebundle.xml;

import java.util.Locale;
import java.util.ResourceBundle;

import org.springframework.context.i18n.LocaleContextHolder;

/**
 * @author k
 */
public class TestReadXml {
    public static void main(String[] args) {
        String key = "user.yes";
        String baseName = "static/i18n/xml/Language";
        Locale locale1 = new Locale("zh", "CN");
        Locale locale2 = new Locale("en", "US");

        ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale1,new XMLResourceBundleControl());
        String string = bundle.getString(key);
        System.out.println("Key: " + string);
        
        ResourceBundle bundle1 = ResourceBundle.getBundle(baseName, locale2,new XMLResourceBundleControl());
        String string1 = bundle1.getString(key);
        System.out.println("Key1: " + string1);
        
        //Spring环境可以自动获取上下文 Local
        String ljw = getConfigValueFromXml(baseName,key);
        if(ljw==null){
            System.out.println("不存在资源key");
        }else{
            System.out.println("ljw:"+ljw);
        }
        
    }
    /**
     * 找 i18nValue 如果没有 返回 i18nKey
     * @param key i18nKey
     * @return 有则返回：i18nValue 没有则返回：null
     */
    private static String getConfigValueFromXml(String baseName,String key){
        String v = null;
        ResourceBundle bundle= ResourceBundle.getBundle(baseName, LocaleContextHolder.getLocale(),new com.k.util.resourcebundle.xml.XMLResourceBundleControl());
        if(bundle.containsKey(key)){
            v = bundle.getString(key);
        }
        return v;
    }
    
    /**
     * 找 i18nValue 如果没有 返回 i18nKey
     * @param key i18nKey
     * @return 有则返回：i18nValue 没有则返回：null
     */
    private static String getConfigValueFromProperties(String baseName,String key){
        String v = null;
        ResourceBundle bundle= ResourceBundle.getBundle(baseName, LocaleContextHolder.getLocale());
        if(bundle.containsKey(key)){
            v = bundle.getString(key);
        }
        return v;
    }
}
